<?php

	namespace App\Models;

    use App\Consts\TableConsts\TableConsts;
	use Illuminate\Database\Eloquent\Factories\HasFactory;
	use Illuminate\Database\Eloquent\Model;

	/* 管理系统用户操作日志类型
	 * 
	 *
	 * developer author	: breezeer @2022.10.08
	 *
	 *
	 */
	class modelSystemOperationLogConstCategory
	{
		# 信用项目
		#
		public const LOG_TYPE_CREDIT_PROJECT												= 1;


        # 普通客户
        public const LOG_TYPE_CLIENT														= 2;
        # 信用客户
        public const LOG_TYPE_CREDIT_CLIENT													= 3;


        # 合同
        public const LOG_TYPE_CONTRACT														= 4;
        # 履约
        public const LOG_TYPE_CONTRACT_FULFILLMENT											= 5;
        # 履约节点
        public const LOG_TYPE_CONTRACT_FULFILLMENT_NODE										= 6;
        # 履约明细
        public const LOG_TYPE_CONTRACT_FULFILLMENT_PROOF									= 7;
        # 履约汇总
        public const LOG_TYPE_CONTRACT_FULFILLMENT_COLLECT									= 8;

        // 案件 调解中心操作日志以 100开头
        public const LOG_TYPE_LAW_CASE                                                      = 101;



	}
	class modelSystemOperationLog extends Model
	{
		use HasFactory;

		#
		#
		protected   $table		    = TableConsts::CY_SYSTEM_OPERATION_LOG_TABLE_NAME			;
		protected   $primaryKey	    = 'id'														;
		public		$timestamps		= false														;
		public function getTableName()
		{ 
			return $this->m_cHelper->gfGetTableName($this->table)								; 
		}
		public function __construct	($userid,$log_type)
		{	
			$this->m_cHelper	= app()->gcHelper												; 
			$this->m_userid		= $userid														;	# 动作行为的生产者id
			$this->m_log_type	= $log_type														;	# 日志分类
		}

		/* 添加一条操作日志
		 *
		 */
		public function addSystemOperationLog($did_something/* 动作行为简要描述 */,$pid=0/* 关联的外部pid */,$remark=''/* 这次动作行为的备注数据 */)
		{
			$this->m_cHelper->gfDbQuery()->insertByArray	(
																$this->m_cHelper->gfGetTableNameByConstKey('CY_SYSTEM_OPERATION_LOG_TABLE_NAME')
																,
																[
																	'userid'			=> $this->m_userid		,
																	'did_something'		=> $did_something		,
																	'log_type'			=> $this->m_log_type	,
																	'pid'				=> $pid					,
																	'remark'			=> $remark				,
																]
															);
		}

		/* 按照时间倒序获取所有的操作日志
		 *
		 */
		public function getSystemOperationLog($pid=0/* 关联的外部pid */,$ar_userid=[]/* 通过用户id过滤 [日志的产生可能是不同的用户,可以查看完整日志,也可以查看具体用户在当前分类下产生的日志] */)
		{
            $build_sql_where_for_search		= $this->m_cHelper->gfDbQuery()->buildSqlWhereWithConditionsAndValue();
			
			# 通过关联的上下文pid进行过滤搜索
			#
			if( $pid>0 )
			{
				$build_sql_where_for_search->appendWhereFilterContidion (" and a.pid=?",$pid);
			}

			# 一个或者更多用户id进行过滤搜索
			#
			if( count($ar_userid)>0 )
			{
				$bm_userid					= $this->m_cHelper->gfDbQuery()->buildSqlMultiParameter($ar_userid);
				$build_sql_where_for_search->appendWhereFilterContidion (" and a.userid in (".$bm_userid->parameter.")",$bm_userid->value);
			}

			# 通过日志分类进行过滤搜索
			#
			$build_sql_where_for_search->appendWhereFilterContidion(" and a.log_type=?",$this->m_log_type);

			# go ...
			#
			$this->m_cHelper->gfDbQuery()->select 
			(
				"
					select
							*
                            ,(select staff_name	from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_STAFF_TABLE_NAME')." where staff_id=a.userid)	as staff_name
					from
							".$this->m_cHelper->gfGetTableNameByConstKey('CY_SYSTEM_OPERATION_LOG_TABLE_NAME')." as a force index(pid)
					where
							".$build_sql_where_for_search->getAllCombineBuildStandarSQL()->sql_where."
					order by a.id desc							
				"
				,
				$build_sql_where_for_search->getAllCombineBuildStandarSQL()->thisx->merge()->getLastSingleArray()
				,
				debug:false
			)
			->forEachRow
			(
				function($row)
				{
					return $row->getOneRowAllData();	
				}
				,
				$ar
			);
			
			#
			#
			return $ar['content'];
		}
	}
